package main;

import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import sprite.Runner;

public class GameCanvas extends Canvas {

    MainCanvas mainCanvas;
    int count;
    int BCount;
    int oppCnt = 0;
    int cnt1 = 0;
    float newScr = 10.0F;
    long time;
    int i;

    GameCanvas(MainCanvas m)
            throws IOException {
        mainCanvas = m;
    }

    protected void paint(Graphics g) {
    }

    boolean HandleGame(int event, int x, int y) {
        switch (event) {
            case 0:
                mainCanvas.mMenuSelect = 0;
                if (CircRectsOverlap(0.9445F * mainCanvas.TX,
                        0.935F * mainCanvas.TY,
                        mainCanvas.mTex_Pause.getWidth() / 2,
                        mainCanvas.mTex_Pause.getHeight() / 2, x, y, 2.0F)) {
                    mainCanvas.mMenuSelect = 1;
                }
                if (x < getWidth() / 2) {
                    if (mainCanvas.mPlayer.Onair == 0) {
                        mainCanvas.mPlayer.Onair = 1;
                        mainCanvas.mPlayer.vy = mainCanvas.Jump;
                        System.out.println("Jump");
                    }

                } else if (mainCanvas.mPlayer.Onair == 0) {
                    mainCanvas.mPlayer.Onair = 2;
                    System.out.println("Roll on land");
                } else {
                    mainCanvas.mPlayer.Onair = 3;
                    System.out.println("Roll on air");
                }
                break;
            case 2:
                if (CircRectsOverlap(0.9445F * mainCanvas.TX,
                        0.915F * mainCanvas.TY,
                        mainCanvas.mTex_Pause.getWidth() / 2,
                        mainCanvas.mTex_Pause.getHeight() / 2,
                        x, y, 2.0F)) {
                    mainCanvas.GameScreen = MainCanvas.GamePause;
                    mainCanvas.soundstop(0);
                    mainCanvas.mMenuSelect = 0;
                }

                if (mainCanvas.mPlayer.Onair == 2) {
                    mainCanvas.mPlayer.Onair = 0;
                }
                break;
        }
        return true;
    }

    boolean HandlePause(int event, int x, int y) {
        if (event == 0) {
            mainCanvas.mMenuSelect = 0;
            if (CircRectsOverlap(100F, 180F,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2,
                    x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 1;
            }

            if (CircRectsOverlap(0.5F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 2;
            }
            if (CircRectsOverlap(0.715F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 3;
            }

        }

        if (event == 2) {
            switch (mainCanvas.mMenuSelect) {
                case 1:
                    if (CircRectsOverlap(0.285F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 3;
                    }
                    break;
                case 2:
                    if (CircRectsOverlap(0.5F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 2;
                    }
                    break;
                case 3:
                    if (CircRectsOverlap(0.715F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.soundState = (!mainCanvas.soundState);
                    }
                    break;
            }
            mainCanvas.mMenuSelect = 0;
        }
        return true;
    }

    boolean HandleGameover(int event, int x, int y) {
        if (event == 0) {
            mainCanvas.mMenuSelect = 0;
            if (CircRectsOverlap(0.4F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 1;
            }
            if (CircRectsOverlap(0.61F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 2;
            }

        }

        if (event == 2) {
            switch (mainCanvas.mMenuSelect) {
                case 1:
                    if (CircRectsOverlap(0.4F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 5;
                    }
                    break;
                case 2:
                    if (CircRectsOverlap(0.61F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 2;
                    }
                    break;
            }
            mainCanvas.mMenuSelect = 0;
        }
        return true;
    }

    boolean HandleOption(int event, int x, int y) {
        if (event == 0) {
            mainCanvas.mMenuSelect = 0;
            if (CircRectsOverlap(0.395F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 1;
            }
            if (CircRectsOverlap(0.605F * mainCanvas.TX,
                    0.69F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 2;
            }
            if (CircRectsOverlap(0.944F * mainCanvas.TX,
                    0.935F * mainCanvas.TY,
                    mainCanvas.mTex_select.getWidth() / 2,
                    mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                mainCanvas.mMenuSelect = 3;
            }
        }
        if (event == 2) {
            switch (mainCanvas.mMenuSelect) {
                case 1:
                    if (CircRectsOverlap(0.395F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.soundState = (!mainCanvas.soundState);
                    }
                    break;
                case 2:
                    if (CircRectsOverlap(0.605F * mainCanvas.TX,
                            0.69F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 8;
                    }
                    break;
                case 3:
                    if (CircRectsOverlap(0.944F * mainCanvas.TX,
                            0.935F * mainCanvas.TY,
                            mainCanvas.mTex_select.getWidth() / 2,
                            mainCanvas.mTex_select.getHeight() / 2, x, y, 2.0F)) {
                        mainCanvas.GameScreen = 2;
                    }
                    break;
            }

            mainCanvas.mMenuSelect = 0;
        }

        return true;
    }

    boolean HandleHelp(int event, int x, int y) {
        mainCanvas.GameScreen = 6;
        return true;
    }

    public void gameLogic() {
        count += 1;
        mainCanvas.BGSPEED -= 1.0E-005F;
        if (mainCanvas.GameScreen != MainCanvas.GameGoing2Over) {
            mainCanvas.mScore += 1;
        }
        if (((mainCanvas.mPlayer.Onair == 0) && (mainCanvas.GameScreen != MainCanvas.GameGoing2Over))
                || (mainCanvas.GameScreen != MainCanvas.GameGoing2Over) || ((mainCanvas.NewScore == 0)
                && (mainCanvas.mScore > mainCanvas.mHScore))) {
            newScr = 1.0F;
            mainCanvas.NewScore += 1;

            BCount = 0;
        }
//Bridge them anh
        for (int k = 0; k < mainCanvas.moWallBase.length; k++) {
            if (mainCanvas.moWallBase[k].x < -0.1F) {//ti le them bridge 0,4/0,6
                if (k > 0) {
                    mainCanvas.moWallBase[k].x = (mainCanvas.moWallBase[(k - 1)].x
                            + mainCanvas.mTex_WallBase.getWidth() / mainCanvas.TX);
                } else {
                    mainCanvas.moWallBase[k].x =
                            (mainCanvas.moWallBase[(mainCanvas.moWallBase.length - 1)].x
                            + mainCanvas.mTex_WallBase.getWidth() / mainCanvas.TX);
                }
            }
            mainCanvas.moWallBase[k].x += mainCanvas.BGSPEED;
        }

        mainCanvas.mPlayer.y += mainCanvas.mPlayer.vy;
        //Xử lý Human action
        Runner runner = mainCanvas.mPlayer;
        runner.vy = ((float) (runner.vy + 0.01D));
        if (mainCanvas.GameScreen == MainCanvas.GamePlay) {
            if ((mainCanvas.mPlayer.Onair == 0) || (mainCanvas.mPlayer.Onair == 1)) {
                if (mainCanvas.mPlayer.y > 1.0F - mainCanvas.mTex_WallBase.getHeight() / mainCanvas.TY - mainCanvas.mTex_Run[0].getHeight() / mainCanvas.TY / 2.0F) {
                    mainCanvas.mPlayer.vy = 0.0F;
                    mainCanvas.mPlayer.y = (1.0F - mainCanvas.mTex_WallBase.getHeight() / mainCanvas.TY - mainCanvas.mTex_Run[0].getHeight() / mainCanvas.TY / 2.0F);
                    if (mainCanvas.mPlayer.Onair == 1) {
                        mainCanvas.mPlayer.Onair = 0;
                    }

                }

            } else if (mainCanvas.mPlayer.y > 1.0F - mainCanvas.mTex_WallBase.getHeight() / mainCanvas.TY - mainCanvas.mTex_Spider[0].getHeight() / mainCanvas.TY / 2.0F) {
                mainCanvas.mPlayer.vy = 0.0F;

                mainCanvas.mPlayer.y = (1.0F - mainCanvas.mTex_WallBase.getHeight() / mainCanvas.TY - mainCanvas.mTex_Spider[0].getHeight() / mainCanvas.TY / 2.0F);
                if ((mainCanvas.mPlayer.Onair == 1) || (mainCanvas.mPlayer.Onair == 3)) {
                    mainCanvas.mPlayer.Onair = 0;
                }

            }

        } else if (mainCanvas.mPlayer.y > 1.2F) {
            mainCanvas.soundstop(0);
            mainCanvas.GameScreen = MainCanvas.GameOver;

            if (mainCanvas.mHScore < mainCanvas.mScore) {
                mainCanvas.mHScore = mainCanvas.mScore;
                mainCanvas.WinnerScore();
            }

        }

        if (BCount == 50) {
            long time = System.currentTimeMillis();
            for (i = 0; i < mainCanvas.moBird1.length; i++) {
                float vx = (float) (time % 20L + 1L);
                time /= 10L;
                float vy = (float) (time % 20L + 1L);
                if (time <= 0L) {
                    time = System.currentTimeMillis();
                }
                mainCanvas.moBird1[i].set(1.0F, 0.7F, -vx / 150.0F, -vy / 150.0F, (int) vx);
            }
        }
        if (BCount == 100) {
            long time = System.currentTimeMillis();
            for (i = 0; i < mainCanvas.moBird2.length; i++) {
                float vx = (float) (time % 10L + 1L);
                time /= 10L;
                float vy = (float) (time % 10L + 1L);
                if (time <= 0L) {
                    time = System.currentTimeMillis();
                }
                mainCanvas.moBird2[i].set(1.2F, randfloat() + 0.02F, -vx / 100.0F, -vy / 100.0F, (int) vx);
            }
        }

        if (BCount >= 180) {
            long time = System.currentTimeMillis();
            for (i = 0; i < mainCanvas.moBird3.length; i++) {
                float vx = (float) (time % 10L + 1L);
                time /= 10L;
                float vy = (float) (time % 10L + 1L);
                if (time <= 0L) {
                    time = System.currentTimeMillis();
                }
                mainCanvas.moBird3[i].set(1.2F, randfloat() + 0.05F, -vx / 150.0F, -vy / 150.0F, (int) vx);
            }

            BCount = 0;
        }

        BCount += 1;
        for (i = 0; i < mainCanvas.moBird1.length; i++) {
            if (count % 3 == 0) {
                mainCanvas.moBird1[i].img += 1;
            }
            mainCanvas.moBird1[i].x += mainCanvas.moBird1[i].vx;
            mainCanvas.moBird1[i].y += mainCanvas.moBird1[i].vy;
        }
        for (i = 0; i < mainCanvas.moBird2.length; i++) {
            if (count % 6 == 0) {
                mainCanvas.moBird2[i].img += 1;
            }
            mainCanvas.moBird2[i].x += mainCanvas.moBird2[i].vx;
            mainCanvas.moBird2[i].y += mainCanvas.moBird2[i].vy;
        }
        for (i = 0; i < mainCanvas.moBird3.length; i++) {
            mainCanvas.moBird3[i].img += 1;
            mainCanvas.moBird3[i].x += mainCanvas.moBird3[i].vx;
            mainCanvas.moBird3[i].y += mainCanvas.moBird3[i].vy;
        }

        long time = System.currentTimeMillis();
        for (i = 0; i < mainCanvas.moDust.length; i++) {
            mainCanvas.moDust[i].x += mainCanvas.moDust[i].vx;
            mainCanvas.moDust[i].y += mainCanvas.moDust[i].vy;
            if ((mainCanvas.moDust[i].x < -1.5F) || (mainCanvas.moDust[i].y < 0.0F)) {
                float vx = (float) (time % 10L + 1L);
                time /= 10L;
                float vy = (float) (time % 10L + 1L);
                if (time <= 0L) {
                    time = System.currentTimeMillis();
                }
                mainCanvas.moDust[i].set(1.0F, randfloat() - 0.01F, -vx * 0.005F, -vy * 0.005F, Math.abs(get_rand()));
            }

        }

        float height = 0.0F;
        if ((mainCanvas.mPlayer.Onair == 0) || (mainCanvas.mPlayer.Onair == 1)) {
            height = mainCanvas.mTex_Run[0].getHeight() / mainCanvas.TY;
//            System.out.println("Heigh Runner: "+height);
        } else {
            height = mainCanvas.mTex_Spider[0].getHeight() / mainCanvas.TY;
//            System.out.println("Heigh Runner: "+height);
        }
        for (i = 0; i < mainCanvas.mOpponent.length; i++) {
            mainCanvas.mOpponent[i].x += mainCanvas.BGSPEED;
            if (mainCanvas.mOpponent[i].x < -1.3F) {
                if (i > 0) {
                    mainCanvas.mOpponent[i].x = (mainCanvas.mOpponent[(i - 1)].x + 1.0F);
                } else {
                    mainCanvas.mOpponent[i].x = (mainCanvas.mOpponent[(mainCanvas.mOpponent.length - 1)].x + 1.0F);
                }
                float x = (float) (time % 10L + 1L);
                time /= 10L;
                if (time <= 0L) {
                    time = System.currentTimeMillis();
                }

                mainCanvas.mOpponent[i].imgNo = ((int) (x % 10.0F));
                mainCanvas.mOpponent[i].OppId = ((int) (x % 3.0F));
            }
            if (mainCanvas.mOpponent[i].OppId == 0) {
                if (CircRectsOverlap(mainCanvas.mOpponent[i].x, mainCanvas.mOpponent[i].y, mainCanvas.mTex_Opp[0].getWidth() / mainCanvas.TX / 3.0F, mainCanvas.mTex_Opp[0].getHeight() / mainCanvas.TY / 2.0F, mainCanvas.mPlayer.x, mainCanvas.mPlayer.y, height / 2.0F)) {
                    if (mainCanvas.GameScreen != 9);
                    mainCanvas.GameScreen = 9;
                    mainCanvas.mPlayer.vy = -0.07F;
                }
            }
            if (mainCanvas.mOpponent[i].OppId == 1) {
                if (CircRectsOverlap(mainCanvas.mOpponent[i].x, mainCanvas.mOpponent[i].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX, mainCanvas.mTex_Opp[0].getWidth() / mainCanvas.TX / 2.0F, mainCanvas.mTex_Opp[0].getHeight() / mainCanvas.TY / 2.0F, mainCanvas.mPlayer.x, mainCanvas.mPlayer.y, height / 2.0F)) {
                    if (mainCanvas.GameScreen == MainCanvas.GamePlay) {
                        mainCanvas.mPlayer.vy = -0.07F;
                    }

                    mainCanvas.GameScreen = MainCanvas.GameGoing2Over;
                }
            }
            if (mainCanvas.mOpponent[i].OppId == 2) {
                if (CircRectsOverlap(mainCanvas.mOpponent[i].x, mainCanvas.mOpponent[i].y, mainCanvas.mTex_Opp[0].getWidth() / mainCanvas.TX / 2.0F, mainCanvas.mTex_Opp[0].getHeight() / mainCanvas.TY / 2.0F, mainCanvas.mPlayer.x, mainCanvas.mPlayer.y, height / 4.0F)) {
                    if (mainCanvas.GameScreen == MainCanvas.GamePlay) {
                        mainCanvas.mPlayer.vy = -0.07F;
                    }
                    mainCanvas.GameScreen = MainCanvas.GameGoing2Over;
                }
                if (CircRectsOverlap(mainCanvas.mOpponent[i].x, 0.01F + mainCanvas.mOpponent[i].y + mainCanvas.OPPDIFF, mainCanvas.mTex_Opp[0].getWidth() / mainCanvas.TX / 2.0F, mainCanvas.mTex_Opp[0].getHeight() / mainCanvas.TY / 2.0F, mainCanvas.mPlayer.x, mainCanvas.mPlayer.y, height / 4.0F)) {
                    if (mainCanvas.GameScreen == MainCanvas.GamePlay) {
                        mainCanvas.mPlayer.vy = -0.07F;
                    }
                    mainCanvas.GameScreen = MainCanvas.GameGoing2Over;
                }
            }
        }
    }
////////////////////////////////////////////////////////////////////////////////    
//~DRAW-------------------------------------------------------------------------

    void DrawGamePlay(Graphics g) {
        g.setColor(115, 130, 150);
        g.fillRect(0, 0, getWidth(), getHeight());

        gameLogic();
        if (mainCanvas.GameScreen == 3) {
            mainCanvas.Soundplay(0);
        }
        g.drawImage(mainCanvas.mTex_BG[0], (int) (mainCanvas.Bg1 * mainCanvas.TX), 0, 20);
        g.drawImage(mainCanvas.mTex_BG[0], (int) (mainCanvas.Bg2 * mainCanvas.TX), 0, 20);
        mainCanvas.Bg1 += mainCanvas.BGSPEED / 3.0F;
        mainCanvas.Bg2 += mainCanvas.BGSPEED / 3.0F;
        if (mainCanvas.Bg1 < -1.0F) {
            mainCanvas.Bg1 = (mainCanvas.Bg2 + 1.0F);
        }
        if (mainCanvas.Bg2 < -1.0F) {
            mainCanvas.Bg2 = (mainCanvas.Bg1 + 1.0F);
        }
//Draw WallBase
        for (int j = 0; j < mainCanvas.moWallBase.length; j++) {
            g.drawImage(mainCanvas.mTex_WallBase, (int) (mainCanvas.moWallBase[j].x * mainCanvas.TX), (int) ((1.0F - mainCanvas.mTex_WallBase.getHeight() / mainCanvas.TY / 2.0F) * mainCanvas.TY), 3);
        }

        g.drawImage(mainCanvas.mTex_Devil[(count % mainCanvas.mTex_Devil.length)], (int) (mainCanvas.mTex_Devil[0].getWidth() / mainCanvas.TX / 2.0F * mainCanvas.TX), 164, 3);

        for (i = 0; i < mainCanvas.moBird3.length; i++) {
            if ((mainCanvas.moBird3[i].x > -1.1F) && (mainCanvas.moBird3[i].x < 1.1F)) {
                g.drawImage(mainCanvas.mTex_Bird3[(mainCanvas.moBird3[i].img % mainCanvas.mTex_Bird3.length)], (int) (mainCanvas.moBird3[i].x * mainCanvas.TX), (int) (mainCanvas.moBird3[i].y * mainCanvas.TY), 3);
            }
        }

        if (mainCanvas.GameScreen == MainCanvas.GameGoing2Over) {
            cnt1 += 1;
            if (cnt1 % 4 == 0) {
                oppCnt += 1;
            }
            g.drawImage(mainCanvas.mTex_Die[(oppCnt % mainCanvas.mTex_Die.length)], (int) (mainCanvas.mPlayer.x * mainCanvas.TX), (int) (mainCanvas.mPlayer.y * mainCanvas.TY), 3); //LEFT|VCENTER
        } //Human drawing-----------------------------------------------------------------
        else if ((mainCanvas.mPlayer.Onair == 0) || (mainCanvas.mPlayer.Onair == 1)) {
            g.drawImage(mainCanvas.mTex_Run[(count % mainCanvas.mTex_Run.length)], (int) (mainCanvas.mPlayer.x * mainCanvas.TX), (int) (mainCanvas.mPlayer.y * mainCanvas.TY), Graphics.VCENTER | Graphics.LEFT);//achor = (4+2)/2 = 3
//            g.drawImage(mainCanvas.mTex_Run[(count % mainCanvas.mTex_Run.length)], 80, 153, Graphics.TOP | Graphics.LEFT);

            System.out.println("Runner X,Y: " + ((int) (mainCanvas.mPlayer.y * mainCanvas.TY)));
        } else {
            g.drawImage(mainCanvas.mTex_Spider[(count % mainCanvas.mTex_Spider.length)], (int) (mainCanvas.mPlayer.x * mainCanvas.TX), (int) (mainCanvas.mPlayer.y * mainCanvas.TY), 3);
        }

        drawOpp(g);
        for (i = 0; i < mainCanvas.moDust.length; i++) {
            g.drawImage(mainCanvas.mTex_Kachra[(mainCanvas.moDust[i].img % mainCanvas.mTex_Kachra.length)], (int) (mainCanvas.moDust[i].x * mainCanvas.TX), (int) (mainCanvas.moDust[i].y * mainCanvas.TY), 3);
        }

        g.drawImage(mainCanvas.mTex_Pause, (int) (0.95F * mainCanvas.TX), (int) (0.930F * mainCanvas.TY), 3);
        if (mainCanvas.mMenuSelect == 1);
        mainCanvas.Draw_number(g, mainCanvas.mScore, (int) (0.0F * mainCanvas.TX), (int) (0.0F * mainCanvas.TY));
    }

    public void DrawMenu(Graphics g) {
        g.drawImage(mainCanvas.mTex_Background, 0, 0, 20);
        mainCanvas.btnStartGame.paint(g);
        mainCanvas.btnAbout.paint(g);
        mainCanvas.btnSetting.paint(g);
        mainCanvas.btnHighScore.paint(g);
        mainCanvas.btnExit.paint(g);
//        switch (mainCanvas.mMenuSelect) {
//            case 1:
//                g.drawImage(mainCanvas.mTex_select, 100, 180, 3);
//                break;
//            case 2:
//                g.drawImage(mainCanvas.mTex_select, 140, 180, 3);
//                break;
//            case 3:
//                g.drawImage(mainCanvas.mTex_select, 180, 180, 3);
//                break;
//            case 4:
//                g.drawImage(mainCanvas.mTex_select, 220, 180, 3);
//        }
    }

    public void DrawGameoverAndPause(Graphics g) {
        g.drawImage(mainCanvas.mTex_BG[0], 0, 0, 20);
        g.drawImage(mainCanvas.mTex_Score, (int) mainCanvas.TX/2, (int)mainCanvas.TY/2, Graphics.HCENTER|Graphics.VCENTER);
        mainCanvas.btnHome.paint(g);
        mainCanvas.btnSetting2.paint(g);
        if (mainCanvas.GameScreen == MainCanvas.GamePause) {
            mainCanvas.btnResume.paint(g);
        } else {
            g.drawImage(mainCanvas.mTex_gameover, (int) (mainCanvas.TX / 2.0F + 1.0F), 207, 3);
            mainCanvas.btnReset.paint(g);
            if (mainCanvas.GameScreen == MainCanvas.GameLoading) {
                mainCanvas.reset();
            }
        }
        String strs = mainCanvas.mScore + "";
        float dx = strs.length() * (mainCanvas.ImgFont.getWidth() / mainCanvas.TX / 2.0F);
        int halfpicsize = (int) ((int) (dx) * mainCanvas.TX) / 2;
        mainCanvas.Draw_number(g, mainCanvas.mScore, (int) mainCanvas.TX / 2 - halfpicsize, (int) mainCanvas.TY / 2+4);
    }

    public void DrawAbout(Graphics g) {
        g.drawImage(mainCanvas.mTex_Aboutus, 0, 0, 20);
        mainCanvas.btnBack.paint(g);
    }

    public void DrawHelp(Graphics g) {
        g.drawImage(mainCanvas.mTex_helpscr, 0, 0, 20);
    }

    public void drawOpp(Graphics g) {
        cnt1 += 1;
        if (cnt1 % 2 == 0) {
            oppCnt += 1;
        }
        for (int j = 0; j < mainCanvas.mOpponent.length; j++) {
            if ((mainCanvas.mOpponent[j].x > -1.1F) && (mainCanvas.mOpponent[j].x < 1.1F)) {
                if (mainCanvas.mOpponent[j].OppId == 0) {
                    switch (mainCanvas.mOpponent[j].imgNo) {
                        case 0:
                            g.drawImage(mainCanvas.mTex_Opp[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            break;
                        case 1:
                            g.drawImage(mainCanvas.mTex_Opp1[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            break;
                        case 2:
                            g.drawImage(mainCanvas.mTex_Opp2[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            break;
                        case 3:
                            g.drawImage(mainCanvas.mTex_Opp3, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            break;
                        case 4:
                            g.drawImage(mainCanvas.mTex_Opp4, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            break;
                        case 5:
                            g.drawImage(mainCanvas.mTex_Opp5[(oppCnt % mainCanvas.mTex_Opp5.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            break;
                        case 6:
                            g.drawImage(mainCanvas.mTex_Opp6[(oppCnt % mainCanvas.mTex_Opp6.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            break;
                        case 7:
                            g.drawImage(mainCanvas.mTex_Opp7[(oppCnt % mainCanvas.mTex_Opp7.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            break;
                        case 8:
                            g.drawImage(mainCanvas.mTex_Opp8[(oppCnt % mainCanvas.mTex_Opp8.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY - 0.05F), 3);
                            break;
                        case 9:
                            g.drawImage(mainCanvas.mTex_Opp9[(oppCnt % mainCanvas.mTex_Opp9.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                    }

                }

                if (mainCanvas.mOpponent[j].OppId == 1) {
                    switch (mainCanvas.mOpponent[j].imgNo) {
                        case 0:
                            g.drawImage(mainCanvas.mTex_Opp[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 1:
                            g.drawImage(mainCanvas.mTex_Opp1[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 2:
                            g.drawImage(mainCanvas.mTex_Opp2[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 3:
                            g.drawImage(mainCanvas.mTex_Opp3, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 4:
                            g.drawImage(mainCanvas.mTex_Opp4, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 5:
                            g.drawImage(mainCanvas.mTex_Opp5[(oppCnt % mainCanvas.mTex_Opp5.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 6:
                            g.drawImage(mainCanvas.mTex_Opp6[(oppCnt % mainCanvas.mTex_Opp6.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 7:
                            g.drawImage(mainCanvas.mTex_Opp7[(oppCnt % mainCanvas.mTex_Opp7.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 8:
                            g.drawImage(mainCanvas.mTex_Opp8[(oppCnt % mainCanvas.mTex_Opp8.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                            break;
                        case 9:
                            g.drawImage(mainCanvas.mTex_Opp9[(oppCnt % mainCanvas.mTex_Opp9.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - mainCanvas.OPPD1 - mainCanvas.mTex_Run[0].getWidth() / 3 / mainCanvas.TX) * mainCanvas.TY), 3);
                    }

                }

                if (mainCanvas.mOpponent[j].OppId == 2) {
                    switch (mainCanvas.mOpponent[j].imgNo) {
                        case 0:
                            g.drawImage(mainCanvas.mTex_Opp[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp1[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF) * mainCanvas.TY), 3);
                            break;
                        case 1:
                            g.drawImage(mainCanvas.mTex_Opp1[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp2[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF) * mainCanvas.TY), 3);
                            break;
                        case 2:
                            g.drawImage(mainCanvas.mTex_Opp2[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp3, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF) * mainCanvas.TY), 3);
                            break;
                        case 3:
                            g.drawImage(mainCanvas.mTex_Opp3, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp4, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF) * mainCanvas.TY), 3);
                            break;
                        case 4:
                            g.drawImage(mainCanvas.mTex_Opp4, (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) (mainCanvas.mOpponent[j].y * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp5[(oppCnt % mainCanvas.mTex_Opp5.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF - 0.03F) * mainCanvas.TY), 3);
                            break;
                        case 5:
                            g.drawImage(mainCanvas.mTex_Opp5[(oppCnt % mainCanvas.mTex_Opp5.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp6[(oppCnt % mainCanvas.mTex_Opp6.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF - 0.03F) * mainCanvas.TY), 3);
                            break;
                        case 6:
                            g.drawImage(mainCanvas.mTex_Opp6[(oppCnt % mainCanvas.mTex_Opp6.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp7[(oppCnt % mainCanvas.mTex_Opp7.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF - 0.03F) * mainCanvas.TY), 3);
                            break;
                        case 7:
                            g.drawImage(mainCanvas.mTex_Opp7[(oppCnt % mainCanvas.mTex_Opp7.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp8[(oppCnt % mainCanvas.mTex_Opp8.length % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF - 0.03F) * mainCanvas.TY), 3);
                            break;
                        case 8:
                            g.drawImage(mainCanvas.mTex_Opp8[(oppCnt % mainCanvas.mTex_Opp8.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp9[(oppCnt % mainCanvas.mTex_Opp9.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF - 0.03F) * mainCanvas.TY), 3);
                            break;
                        case 9:
                            g.drawImage(mainCanvas.mTex_Opp9[(oppCnt % mainCanvas.mTex_Opp9.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y - 0.015F) * mainCanvas.TY), 3);
                            g.drawImage(mainCanvas.mTex_Opp[(oppCnt % mainCanvas.mTex_Opp.length)], (int) (mainCanvas.mOpponent[j].x * mainCanvas.TX), (int) ((mainCanvas.mOpponent[j].y + mainCanvas.OPPDIFF) * mainCanvas.TY), 3);
                    }
                }
            }
        }
    }
////////////////////////////////////////////////////////////////////////////////
//                                  ULTILITIES                                //
////////////////////////////////////////////////////////////////////////////////

    /**
     *
     * @param centerX
     * @param centerY
     * @param radius
     * @param RectX
     * @param RectY
     * @param RectDX
     * @param RectDY
     * @return
     */
    boolean intersectCircleRect(int centerX, int centerY, int radius, int RectX, int RectY, int RectDX, int RectDY) {
        if ((abs(centerX - (RectX + RectDX / 2)) <= RectDX / 2 + radius) && (abs(centerY - (RectY + RectDY / 2)) <= RectDY / 2 + radius)) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param x
     * @return
     */
    int abs(int x) {
        return x > 0 ? x : -x;
    }

    /**
     *
     * @param x1
     * @param y1
     * @param r1
     * @param x2
     * @param y2
     * @param r2
     * @return
     */
    boolean cirInrSect(int x1, int y1, int r1, int x2, int y2, int r2) {
        int d = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
        if (d < r1 + r2) {
            return true;
        }
        return false;
    }

    int get_rand() {
        Random r = new Random();
        int myNum = (r.nextInt() & 0xFFFF) * 500 >> 16;
        return myNum;
    }

    float randfloat() {
        Random r = new Random();
        return r.nextFloat();
    }

    int sqrt(int x) {
        if (x < 3) {
            return 1;
        }

        for (int i = 2; i <= x / 2 + 1; i++) {
            if (x == i * i) {
                return i;
            }
            if (x < i * i) {
                return i - 1;
            }
        }
        return x;
    }

    /**
     *
     * @param CRX
     * @param CRY
     * @param CRDX
     * @param CRDY
     * @param centerX
     * @param centerY
     * @param radius
     * @return
     */
    boolean CircRectsOverlap(float CRX, float CRY, float CRDX, float CRDY, float centerX, float centerY, float radius) {
        if ((Math.abs(centerX - CRX) <= CRDX + radius) && (Math.abs(centerY - CRY) <= CRDY + radius)) {
            return true;
        }
        return false;
    }

    /**
     *
     * @param a
     * @return
     */
    float screen2worldX(float a) {
        return a / getWidth();
    }

    /**
     *
     * @param a
     * @return
     */
    float screen2worldY(float a) {
        return a / getHeight();
    }
}